/***********************************/
/** Virtual NC Machine 13.02.2012 **/
/** ADDED MAGASINE 27.1.2013      **/
/***********************************/
#ifndef __mNC__
#define __mNC__

//ROTATE
#define ROTATE_X 1001
#define ROTATE_Y 1002
#define ROTATE_Z 1003
#define ROTATE_ANGLE 1004


//FANUC
#define MacroAlarm 3000 //
#define MacroAlarmMessage 3006 //
//time information
#define TimeAfterPowerOn 3001
#define TimeCycleLampOn 3002
#define TimeDAY 3011
#define TimeHoursMinutesSeconds 3012

//Automatic operation control
#define ControlState 3003
#define ControlStateFeed 3004

//Setting
#define Settings 3005

//mirror image protected
#define MirrorImage 3007 //Number of machined parts 0000001=fist axis jne...

//PERSONAL NOT FANUC
#define MotionDist 3500
#define MotionTime 3501
#define RapidDist 3502
#define RunTime 3503
#define CurrentFeed 3504
#define CurrentTool 3505
//NUMBER OF MACHINED PARTS can be read and written.
#define CompletionNumber 3901 //Number of machined parts
#define TargetNumber 3902 //Number of required parts

//modal information
#define Group1 4001 //G00,G01,G02,G03,G33
#define Group2 4002 //G17,G18,G19
#define Group3 4003 //G90,G91
#define Group4 4004 //
#define Group5 4005 //G94,G95
#define Group6 4006 //G20,G21
#define Group7 4007 //G40,G41,G42
#define Group8 4008 //G43,G44,G49
#define Group9 4009 //G73,G74,G76,G80-G89
#define Group10 4010 //G98,G99
#define Group11 4011 //G50,G51
#define Group12 4012 //G66,G67
#define Group13 4013 //G69,G97
#define Group14 4014 //G54-G59
#define Group15 4015 //G61-G64
#define Group16 4016 //G68,G69
#define Group22 4022 //

#define Acode 4101 //A
#define Bcode 4102 //B code
#define Ccode 4103 //C
#define Dcode 4104 //D code
#define Ecode 4105 //E
#define Fcode 4106 //F code
#define Gcode 4107 //G
#define Hcode 4108 //H
#define Icode 4109 //I
#define Jcode 4110 //J code
#define Kcode 4111 //K code
#define Lcode 4112 //L
#define Mcode 4113 //M
#define Ncode 4114 //N code
#define Ocode 4115 //O code
#define sequencenumber 4114 //
#define programnumber 4115 //
#define Pcode 4116 //P number of currenty selected additional workpiece coordninate system
#define Qcode 4117 //Q
#define Rcode 4118 //R code
#define Scode 4119 //S code
#define Tcode 4120 //T
#define Ucode 4121 //U
#define Vcode 4122 //V code
#define Wcode 4123 //W
#define Xcode 4124 //X
#define Ycode 4125 //Y
#define Zcode 4126 //Z

//********************//
#define Kcanned 4201 //X
#define Rcanned 4202 //Y
#define Zcanned 4203 //Z

//Current position CAN ONLY BE READ
#define BEP_X 5001 //X Block end point,Workpiece coordinate system.
#define BEP_Y 5002 //Y Block end point,Workpiece coordinate system.
#define BEP_Z 5003 //Z Block end point,Workpiece coordinate system.
#define BEP_A 5004 //A Block end point,Workpiece coordinate system.

//CENTER POSITION.......rpm
#define BEP_I 5005 //I Block arc center point,Workpiece coordinate system.
#define BEP_J 5006 //J Block arc center point,Workpiece coordinate system.
#define BEP_K 5007 //K Block arc center point,Workpiece coordinate system.
#define BEP_R 5008 //K Block arc center point,Workpiece coordinate system.

#define CPM_X 5021 //X Current position,Machine coordinate system.
#define CPM_Y 5022 //Y Current position,Machine coordinate system.
#define CPM_Z 5023 //Z Current position,Machine coordinate system.
#define CPM_A 5024 //A Current position,Machine coordinate system.

#define CP_X 5041 //X Current position,Workpiece coordinate system.
#define CP_Y 5042 //Y Current position,Workpiece coordinate system.
#define CP_Z 5043 //Z Current position,Workpiece coordinate system.
#define CP_A 5044 //A Current position,Workpiece coordinate system.

#define SSP_X 5061 //X Skip signal position,Workpiece coordinate system.
#define SSP_Y 5062 //Y Skip signal position,Workpiece coordinate system.
#define SSP_Z 5063 //Z Skip signal position,Workpiece coordinate system.
#define SSP_A 5064 //A Skip signal position,Workpiece coordinate system.

#define TLOV_X 5081 //X Tool offset value.
#define TLOV_Y 5082 //Y
#define TLOV_Z 5083 //Z
#define TLOV_A 5084 //A

#define DSP_X 5101 //X Deviated servo position
#define DSP_Y 5102 //Y
#define DSP_Z 5103 //Z
#define DSP_A 5104 //A

//Workpiece coordinate system compensation values (workpiece zero point offset values)
//CAN BE READ AND WRITTEN
#define EXT_X 5201 //X external workpiece zero point offset value
#define EXT_Y 5202 //Y external workpiece zero point offset value
#define EXT_Z 5203 //Z external workpiece zero point offset value
#define EXT_A 5204 //A external workpiece zero point offset value

#define G54_X 5221 //X workpiece zero point offset value
#define G54_Y 5222 //Y workpiece zero point offset value
#define G54_Z 5223 //Z workpiece zero point offset value
#define G54_A 5224 //A workpiece zero point offset value

#define G55_X 5241 //X workpiece zero point offset value
#define G55_Y 5242 //Y workpiece zero point offset value
#define G55_Z 5243 //Z workpiece zero point offset value
#define G55_A 5244 //A workpiece zero point offset value

#define G56_X 5261 //X workpiece zero point offset value
#define G56_Y 5262 //Y workpiece zero point offset value
#define G56_Z 5263 //Z workpiece zero point offset value
#define G56_A 5264 //A workpiece zero point offset value

#define G57_X 5281 //X workpiece zero point offset value
#define G57_Y 5282 //Y workpiece zero point offset value
#define G57_Z 5283 //Z workpiece zero point offset value
#define G57_A 5284 //A workpiece zero point offset value

#define G58_X 5301 //X workpiece zero point offset value
#define G58_Y 5302 //Y workpiece zero point offset value
#define G58_Z 5303 //Z workpiece zero point offset value
#define G58_A 5304 //A workpiece zero point offset value

#define G59_X 5321 //X workpiece zero point offset value
#define G59_Y 5322 //Y workpiece zero point offset value
#define G59_Z 5323 //Z workpiece zero point offset value
#define G59_A 5324 //A workpiece zero point offset value

#define G541P_X 7001 //X NEXT-- P2 = 7001+20
#define G541P_Y 7002 //Y workpiece zero point offset value
#define G541P_Z 7003 //Z workpiece zero point offset value
#define G541P_A 7004 //A workpiece zero point offset value

//Tool compensation values
#define H_Geometric_compensation 11001 //1--400
#define H_Wear_compensation 10001		//

#define D_Geometric_compensation 13001 //
#define D_Wear_compensation 12001		//

//*******************************************************************************//
//***
//*******************************************************************************//
#include <stdio.h>
#include <iostream>
#include <wx/string.h>
#include <wx/textfile.h>

#include "mPointArray.h"

#include <cassert>
#include "vertex.h"
#include "mSolver.h"
#include <wx/tokenzr.h>
#include "wx/intl.h"

#define numTools  41

//*************************//
//**    CONTROL CLASS    **//
//*************************//
class mNC : public mSolver
{
private:
    double V[20000];
    double time;

    void G0(double,double,double);
    void G1(double,double,double);
    void G2(double,double,double,double,double,double);
    void G3(double,double,double,double,double,double);

    wxString LoadFile(wxString);

public:
    std::vector<vertex> I;                               //RUN DATA LOG AND FOR VBO CREATION

    void addP(mPoint p,uint G,double Aika,double Tool);

    //Image T[numTools+1];                                //TOOL MAGASINE

    wxString PROGPATH,MSG,ERR;

    mPoint pos;                            //current position
    int tool;                              //current tool
    double feed;
    double aika;

    mNC();
    ~mNC();

    //*******************//
    void init();

    //info
    double GetCycleTime();
    bool Tick(double add=0.0006666666666666666); //default sekunnin 25 osa
    bool SetTime(double setaika);
    bool GetPos();

    //**********************//
    //void Render();
    //*****************//
    void ToV(int,double);
    double GetV(int);

    //Constructor
    void Run(wxString);
    void SubRun(wxString);
    void CYCLE(wxString);

};
#endif // __Contour__
